Multiple layouts in rails [Newbie Q]
Posted
by BriteLite
on Stack Overflow
See other posts from Stack Overflow
or by BriteLite
Published on 2010-06-15T03:47:49Z
Indexed on
2010/06/15
3:52 UTC
Read the original article
Hit count: 235
ruby-on-rails
Hi. As a newb, I decided to build a "home inventory" application. I am now stuck on how to programmatically select a layout based on what type of item it is when viewing it in a browser.
According to my planning, so far I should have created a few models to represent types of items I can find in my home: Furniture, Electronics and Books.
class Book < ActiveRecord::Base
end
class Furniture < ActiveRecord::Base
end
class Electronic < ActiveRecord::Base
end
Now the Books model has things like isbn, pages, address, and category. Furniture model has things like color, price, address, and category. Electronics has things like name, voltage, address, and category.
Here is where I got confused. I know the property address is going to be the same for all of them. I also know that, I will need to create multiple "layouts" for 3 different types of items to show the different properties of said items with appropriate graphics and stylesheets.
But how will I go about deciding which category the item is so I can determine which layout to render.
According to me, this is how I will do it:
class DisplayController < ApplicationController
def display
@item = Params[:item]
if @item.category = "electronics"
render :layout => 'electronics'
end
end
In my routes.rb
map.display ':item', :controller => 'display', :action => 'display'
I only seem to have one concern with this, I probably will add a lot of categories later on and think there should be a more DRY-esque way of dealing, rather than hardcoding them.
I understand that I need to add into my layout html tags to display relevant information for that particular category.
----Questions----
- Is this the right way to approach this type of problem.
- Will this approach be compatible when I decide to add a gem like *thinking_sphinx* to run search.
- What issues do you see with my approach and how can I make it better.
- I was reading something about "Polymorphic Assoc", does that apply in this case, since category exist for all items?
Also, I was trying to get a routes to render a URL like "http://localhost/living-room-tv"
© Stack Overflow or respective owner